package com.ruoyi.planthopper.test;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

import com.ruoyi.planthopper.utils.FileTool;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;


import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: base-planthopper-detection-master_202130504106_李航
 * @description:
 * @author: LiHang
 * @create: 2021-09-24 08:32
 **/

@SpringBootTest
public class test {

    @Test
    public void testFileExist(){
        File file = new File("G:\\学习\\研究生\\code\\基部飞虱检测\\imageSave/detection/2021/09/25/7b1a8106-23e0-4f68-a915-6a7504b076bb__original__Result.jpg");
        File newFileResult=new File("G:\\学习\\研究生\\code\\基部飞虱检测\\imageSave\\detection\\2021\\09\\25\\飞蛾_2020-08-19-09-09-39_台风天_江苏_南工程用田_李航8_小米12手机__original__Result.jpg");
        if (!file.exists()) {
            System.out.println("1");
        }else {
            System.out.println("2");
        }
        if (!file.exists()) {
            System.out.println("原始文件不存在");
        }else {
            System.out.println("原始文件存在");
            if(!newFileResult.exists()){
                boolean test3=file.renameTo(newFileResult);
                System.out.println(test3);
            }else{
                System.out.println("新的名字已存在");
            }

        }

    }

    /**
     * 测试FileTool工具
     */
    @Test
    public void testFileTool(){
        File file = new File("G:\\学习\\研究生\\code\\基部飞虱检测\\2babb390-53d8-420f-9afb-52e9d17a8345__original.jpg");
        System.out.println(FileTool.getImgCreateTime(file));
    }


    /**
     * 用这个
     * 可以查询到所有的图片信息，然后具体要某个就可以用下面这种方式或者用if来获取
     */
    @Test
    public void imageInfo2() throws ImageProcessingException, IOException {
        String imageName="";
        File file = new File("G:\\学习\\研究生\\code\\基部飞虱检测\\2babb390-53d8-420f-9afb-52e9d17a8345__original.jpg");
        String[] array = new String[3];
        //如果你对图片的格式有限制，可以直接使用对应格式的Reader如：JPEGImageReader
        ImageMetadataReader.readMetadata(file)
                .getDirectories().forEach(v ->
                        v.getTags().forEach(t -> {
                            //System.out.println(t.getTagName() + " ： " + t.getDescription());
                            switch (t.getTagName()) {
                                //                    经度
                                case "GPS Longitude":
                                    array[0] = t.getDescription();
                                    break;
                                //                        纬度
                                case "GPS Latitude":
                                    array[1] = t.getDescription();
                                    break;
                                //                        拍摄时间
                                case "Date/Time Original":
                                    array[2] = t.getDescription();
                                default:
                                    break;
                            }
                        })
                );
        imageName=array[2];
        imageName=imageName.split(" ")[0].replace(":","-")+" "+imageName.split(" ")[1];
        System.out.println(imageName);
        System.out.println(array[2]);
    }

    /**
     * 获取到所有图片数据
     * 备用方案。不推荐，没使用举例
     * @throws JpegProcessingException
     * @throws IOException
     */
    @Test
    public void imageInfo1() throws JpegProcessingException, IOException {
        File img = new File("G:\\学习\\研究生\\code\\基部飞虱检测\\2babb390-53d8-420f-9afb-52e9d17a8345__original.jpg");
        System.out.println("File Name:" + img.getName());

        Metadata metadata = JpegMetadataReader.readMetadata(img);
        System.out.println("Directory Count: "+metadata.getDirectoryCount());
        System.out.println();

        //输出所有附加属性数据
        for (Directory directory : metadata.getDirectories()) {
            System.out.println("******\t" + directory.getName() + "\t******");
            for (Tag tag : directory.getTags()) {
                System.out.println(tag.getTagName() + ":" + tag.getDescription());
            }
            System.out.println();
            System.out.println();
        }

    }

    /**
     * 测试Date
     */
    @Test
    public void testDate() throws ParseException {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        System.out.println(sdf.format(date));
        //String string = "2016-10-24 21:59:06";
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //System.out.println(sdf.parse(string));
    }

    /**
     *
     * @param
     * @return 返回Date类型
     * @throws ImageProcessingException
     * @throws IOException
     */
    @Test
    public void ImageShootingTimeDate() throws ImageProcessingException, IOException, ParseException {
        File file = new File("G:\\学习\\研究生\\code\\基部飞虱检测\\2babb390-53d8-420f-9afb-52e9d17a8345__original.jpg");
        String[] array = new String[3];
        //如果你对图片的格式有限制，可以直接使用对应格式的Reader如：JPEGImageReader
        ImageMetadataReader.readMetadata(file)
                .getDirectories().forEach(v ->
                        v.getTags().forEach(t -> {

                            switch (t.getTagName()) {
                                case "Date/Time Original":
                                    array[2] = t.getDescription();
                                default:
                                    break;
                            }
                        })
                );
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        Date date =sdf.parse(array[2]);
        System.out.println(date);
        //return date;
    }

}
